package com.wisecoach.gatewayplus.transaction;

import org.hyperledger.fabric.client.CommitStatusException;
import org.hyperledger.fabric.client.Status;
import org.hyperledger.fabric.client.SubmittedTransaction;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * {@code @author:} wisecoach
 * {@code @date:} 2023/9/27 下午7:02
 * {@code @version:} 1.0.0
 */


public abstract class AbstractSubmittedTxProcessor implements SubmittedTxProcessor {

    private static final Executor executor = Executors.newCachedThreadPool();

    @Override
    public void process(SubmitTxContext transaction) {
        executor.execute(() -> {
            try {
                SubmittedTransaction submittedTransaction = transaction.getSubmittedTransaction();
                // 阻塞等待上链结果
                Status status = submittedTransaction.getStatus();
                // 处理上链结果
                doProcess(transaction, status);
            } catch (CommitStatusException e) {
                throw new RuntimeException(e);
            }
        });
    }

    protected abstract void doProcess(SubmitTxContext ctx, Status status);
}
